#include "engine.h"
#include "comp_mgr.h"
#include <vector>
#include <string>

#include <GL/glfw.h>

namespace HW
{

int Initialize(int allowenginedebug)
{
	if ( glfwInit() == GL_FALSE )
		return 0;

	std::string lEngineName = "Hippowerks Engine ";
	lEngineName.append ( _HW_VER_ );
	
	extern void _i_InitFileSystem ( void );
	extern void _i_InitGraphics(void);
	extern void _i_InitLog ( const_str logFileName );
	extern void _i_InitBuffers ( void );
	extern void _i_InitKeyMap ( void );
	extern void _i_InitSurfaces ( void );

	ComponentManager::PreInit();
	if ( !ComponentManager::Init() )
		return 0;

	_i_InitLog ( "Engine.txt" );
	AppLog ( lEngineName.c_str() );
	_i_InitFileSystem();
	_i_InitGraphics();
	_i_InitBuffers();
	_i_InitSurfaces();
	_i_InitKeyMap ( );

	return 1;
}

int Terminate(void)
{
	// TODO: Free all the resources here

	AppLog ( "Terminate function" );

	glfwTerminate ( );

	extern void _i_UnInitLog(void);
	extern void _i_UnInitTextures ( void );
	extern void _i_UnInitBuffers ( void );
	extern void _i_UnInitShaders ( void );
	extern void _i_UnInitSurfaces ( void );
	extern void _i_UnInitMaterials ( void );

	_i_UnInitMaterials();
	_i_UnInitShaders();
	_i_UnInitSurfaces();
	_i_UnInitBuffers();
	_i_UnInitTextures();
	_i_UnInitLog ( );

	ComponentManager::DeInit();

	return 0;
}

void SetAppTitle(const_str title)
{
	extern char gAppTitle[100];
	int i;
	for ( i = 0 ; ( i < 100 ) && ( title[i]!= NULL ) ; ++i )
		gAppTitle[i] = title[i];
	gAppTitle[i] = NULL;
}

int AppTerminate(void)
{
	extern int gAppTerminated;
	return gAppTerminated;
}

}